<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="提现申请日期" prop="startDate">
        <el-input
          v-model="queryParams.startDate"
          placeholder="请输入商户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现申请日期" prop="endDate">
        <el-input
          v-model="queryParams.endDate"
          placeholder="请输入商户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户名称" align="center" prop="businessName" />
      <el-table-column label="提现金额" align="center" prop="applyAmount" />
      <el-table-column label="申请日期" align="center" prop="applyDate" />

    </el-table>
    <el-upload
      class="avatar-uploader"
      action="http://localhost/dev-api/finance/apply/upload"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload">
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或充值提现申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="充值或提现申请单号" prop="applyNo">
          <el-input v-model="form.applyNo" placeholder="请输入充值或提现申请单号" />
        </el-form-item>
        <el-form-item label="申请类型， 1：充值，2：提现" prop="applyType">
          <el-select v-model="form.applyType" placeholder="请选择申请类型， 1：充值，2：提现">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请日期" prop="applyDate">
          <el-date-picker clearable size="small"
            v-model="form.applyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请提现金额" prop="applyAmount">
          <el-input v-model="form.applyAmount" placeholder="请输入申请提现金额" />
        </el-form-item>
        <el-form-item label="提现到账户ID(提现的时候必须)" prop="accountDetailId">
          <el-input v-model="form.accountDetailId" placeholder="请输入提现到账户ID(提现的时候必须)" />
        </el-form-item>
        <el-form-item label="开户银行" prop="brankName">
          <el-input v-model="form.brankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="开户支行" prop="brankNameChildren">
          <el-input v-model="form.brankNameChildren" placeholder="请输入开户支行" />
        </el-form-item>
        <el-form-item label="账户名" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入账户名" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="brankId">
          <el-input v-model="form.brankId" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="审核状态， 1:待审核，2：审核通过，3：审核不通过" prop="checkState">
          <el-input v-model="form.checkState" placeholder="请输入审核状态， 1:待审核，2：审核通过，3：审核不通过" />
        </el-form-item>
        <el-form-item label="审核时间" prop="checkDate">
          <el-date-picker clearable size="small"
            v-model="form.checkDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人ID" prop="checkUserId">
          <el-input v-model="form.checkUserId" placeholder="请输入审核人ID" />
        </el-form-item>
        <el-form-item label="审核人" prop="checkUserName">
          <el-input v-model="form.checkUserName" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="处理状态， 1:处理中，2：提现成功，3：提现失败， 4：充值成功, 5：充值失败" prop="processState">
          <el-input v-model="form.processState" placeholder="请输入处理状态， 1:处理中，2：提现成功，3：提现失败， 4：充值成功, 5：充值失败" />
        </el-form-item>
        <el-form-item label="支付流水号" prop="payFlowNo">
          <el-input v-model="form.payFlowNo" placeholder="请输入支付流水号" />
        </el-form-item>
        <el-form-item label="到账时间" prop="dzDate">
          <el-date-picker clearable size="small"
            v-model="form.dzDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择到账时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留字段5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
        <el-form-item label="删除标识,0:未删除，1：删除" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识,0:未删除，1：删除" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户ID" />
        </el-form-item>
        <el-form-item label="更新用户ID" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFind, getApply, delApply, addApply, updateApply } from "@/api/finance/apply";

export default {
  name: "Apply",
  components: {
  },
  data() {
    return {
      imageUrl: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户充值提现申请表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: null,
        endDate: null,
        applyNo: null,
        applyType: null,
        applyDate: null,
        applyAmount: null,
        accountDetailId: null,
        brankName: null,
        brankNameChildren: null,
        accountName: null,
        brankId: null,
        checkState: null,
        checkDate: null,
        checkUserId: null,
        checkUserName: null,
        processState: null,
        payFlowNo: null,
        dzDate: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applyNo: [
          { required: true, message: "充值或提现申请单号不能为空", trigger: "blur" }
        ],
        applyDate: [
          { required: true, message: "申请日期不能为空", trigger: "blur" }
        ],
        applyAmount: [
          { required: true, message: "申请提现金额不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    /** 查询商户充值提现申请列表 */
    getList() {
      this.loading = true;
      listFind(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        applyNo: null,
        applyType: null,
        applyDate: null,
        applyAmount: null,
        accountDetailId: null,
        brankName: null,
        brankNameChildren: null,
        accountName: null,
        brankId: null,
        checkState: null,
        checkDate: null,
        checkUserId: null,
        checkUserName: null,
        processState: null,
        payFlowNo: null,
        dzDate: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户充值提现申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "充值提现申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商户充值提现申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApply(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/apply/export', {
        ...this.queryParams
      }, `finance_apply.xlsx`)
    }
  }
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
